package com.woorigil.social.parser;

import java.io.IOException;
import java.io.Reader;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.ParsingException;
import nu.xom.ValidityException;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.woorigil.social.DateUtils;
import com.woorigil.social.domain.ProviderCode;
import com.woorigil.social.domain.deal.Deal;
import com.woorigil.social.domain.deal.DealListDocument;
import com.woorigil.social.domain.deal.ManageStatus;

@Component (value="tmonXmlParser")
public class TmonXmlParser extends XmlParser {
	@Value("${tmon.deal.path}") 
	String dealPath;

	@Override
	public String getCharsetName() {
		return "EUC-KR";
	}

	@Override
	public ProviderCode getProviderCode() {
		return ProviderCode.TM;
	}
	
	@Override
	public String getSourcePath() {
		return dealPath;
	}

	@Override
	public DealListDocument parse(Reader reader) throws ValidityException, ParsingException, IOException {
		Builder builder = new Builder();
		Document doc = builder.build(reader);
		Element root = doc.getRootElement();

		DealListDocument dealList = new DealListDocument();
		dealList.setSourceProvider("티몬");
		dealList.setVersion("0.0");
		
		Elements dealsEles = root.getChildElements();
		for (int i = 0; i < dealsEles.size(); i++) {
			if (i == 0) {
				continue;
			}
			Element dealEl = dealsEles.get(i);

			Deal deal = new Deal();
			deal.setProviderCode(getProviderCode());
			deal.setMetaId(extractMetaId(getValue(dealEl.getFirstChildElement("link"))));
			deal.setCategory(getValue(dealEl.getFirstChildElement("category")));
			deal.setSaleStartAt(DateUtils.toDate(getValue(dealEl.getFirstChildElement("time_start")), "yyyy-MM-dd HH:mm:ss"));//2012-11-29 00:00:00
			deal.setSaleEndAt(DateUtils.toDate(getValue(dealEl.getFirstChildElement("time_end")), "yyyy-MM-dd HH:mm:ss"));
			deal.setTitle(getValue(dealEl.getFirstChildElement("title")));
			deal.setWebLink(getValue(dealEl.getFirstChildElement("link")));
			deal.setOriginalPrice(NumberUtils.toInt(getValue(dealEl.getFirstChildElement("price_original")), 0));
			deal.setSalesPrice(NumberUtils.toInt(getValue(dealEl.getFirstChildElement("price_now")), 0));
			deal.setDiscountRate(NumberUtils.toInt(getValue(dealEl.getFirstChildElement("sale_percent")), 0));
			deal.setMaxCount(NumberUtils.toInt(getValue(dealEl.getFirstChildElement("count_max")), 0));
			deal.setMinCount(NumberUtils.toInt(getValue(dealEl.getFirstChildElement("count_min")), 0));
			deal.setNowCount(NumberUtils.toInt(getValue(dealEl.getFirstChildElement("sell_count")), 0));
			deal.setSalesStatus(getValue(dealEl.getFirstChildElement("type")));
			deal.setImageUrl1(getValue(dealEl.getFirstChildElement("photo1")));
			deal.setShopLatitude(getValue(dealEl.getFirstChildElement("latitude")));
			deal.setShopLongitude(getValue(dealEl.getFirstChildElement("longitude")));
				
			deal.setImageUrl(deal.getImageUrl1());
			deal.setAverageCount((int)(deal.getNowCount() / DateUtils.getTodayCount(deal.getSaleStartAt())));
			deal.setManageStatus(ManageStatus.S);
			deal.setCategoryId(findCategoryId(getProviderCode(), deal));
			dealList.add(deal);
		}
		
		return dealList;
	}

	protected String extractMetaId(String value) {
		if (value != null && value.length() > 6) {
			return value.substring(value.lastIndexOf("&p_no=") + 6, value.length());
		}
		return null;
	}
}
